#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <byteswap.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <fcntl.h>
#include <ctype.h>
#include <termios.h>

#include <sys/types.h>
#include <sys/mman.h>

#define FATAL do { fprintf(stderr, "Error at line %d, file %s (%d) [%s]\n", __LINE__, __FILE__, errno, strerror(errno)); exit(1); } while(0)

#define MAP_SIZE (32*1024UL)
#define MAP_MASK (MAP_SIZE - 1)

unsigned long long g_word;

int reg_bypass_rw(int bypass,unsigned int address) {
	void *map_base;
	uint64_t read_result;
	unsigned int baseaddr=0;

	baseaddr = address;
	/* map one page */
	map_base = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, bypass, 0);
	if (map_base == (void *) -1)
		FATAL
		;
	read_result = *((uint64_t *) map_base);
	g_word=read_result;

	if (munmap(map_base, MAP_SIZE) == -1)
		FATAL
		;
	return 0;
}

int reg_bypass_control(int bypass,uint64_t data)
{
	void *map_base;
	unsigned int baseaddr=0;

	baseaddr = 0x0;
	/* map one page */
	map_base = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, bypass, 0);
	if (map_base == (void *) -1)
		FATAL
		;
	*((uint64_t *) map_base)=data;

	if (munmap(map_base, MAP_SIZE) == -1)
		FATAL
		;
	return 0;
}
